<template>
    <div class="page-container" v-loading="loading">
        <div class="page-filter-option mb-3">
            <el-form :inline="true" :model="filterOption" class="flex flex-wrap">
                <el-form-item>
                    <el-input :placeholder="$t('form.inputPlaceholder')" suffix-icon="el-icon-search" class="input-search-select"
                              v-model="filterOption.keyword" @change="filterChange" clearable>
                        <el-select slot="prepend" v-model="filterOption.keyword_column">
                            <el-option :label="$t('game.config.currency.currency')" value="currency"></el-option>
                            <el-option :label="$t('game.config.currency.description')" value="description"></el-option>
                        </el-select>
                    </el-input>
                </el-form-item>
                <el-form-item class="small">
                    <el-select :placeholder="$t('game.config.currency.type')" v-model="filterOption.type" @change="filterChange" clearable>
                        <el-option v-for="(item, index) in $t('game.config.currency.typeList')"
                                   :key="index" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item class="small">
                    <el-select :placeholder="$t('game.config.currency.status')" v-model="filterOption.status" @change="filterChange" clearable>
                        <el-option v-for="(item, index) in $t('form.switchList')"
                                   :key="index" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <component-page-option-datetime-range type="datetimerange" align="center"
                                                          v-model="filterOption.datetime" @change="filterChange">
                    </component-page-option-datetime-range>
                </el-form-item>
                <el-form-item>
                    <el-button @click="filterChange">{{ $t('action.search') }}</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="page-container-inner-fluid flex-auto flex flex-col">
            <component-page-content-resize v-slot="{ content }">
                <el-table style="width: 100%" :height="content.height"
                          :data="dataList"
                          :default-sort="{prop: filterOption.order_column, order: filterOption.order_key}"
                          @sort-change="filterOrderChange"
                          highlight-current-row>
                    <el-table-column min-width="150" :label="$t('game.config.currency.type')" prop="type">
                        <template slot-scope="scope">
                            <el-tag effect="plain" size="mini" type="info">
                                {{ $t('game.config.currency.typeList')[scope.row.type].label }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column min-width="150" :label="$t('game.config.currency.currency')" prop="currency"></el-table-column>
                    <el-table-column min-width="180" :label="$t('game.config.currency.description')" prop="description"></el-table-column>
                    <el-table-column min-width="150" :label="$t('game.config.currency.status')" prop="status">
                        <template slot-scope="scope">
                            <el-tag effect="plain" size="mini" type="info">
                                {{ $t('form.switchList')[scope.row.status].label }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column min-width="220" :label="$t('game.config.currency.created')" prop="created" sortable="custom" :sort-orders="['descending', 'ascending']">
                        <template slot-scope="scope">
                            <component-page-timestamp :timestamp="scope.row.created"></component-page-timestamp>
                        </template>
                    </el-table-column>
                    <el-table-column min-width="220" :label="$t('game.config.currency.updated')" prop="updated">
                        <template slot-scope="scope">
                            <component-page-timestamp :timestamp="scope.row.updated"></component-page-timestamp>
                        </template>
                    </el-table-column>
                    <el-table-column width="100" :label="$t('game.config.currency.operation')" align="right" fixed="right">
                        <template slot-scope="scope">
                            <span class="operation-options-icon">
                                <i class="el-icon-refresh" @click="dialogRefresh(scope)"></i>
                            </span>
                        </template>
                    </el-table-column>
                </el-table>
            </component-page-content-resize>
            <div class="text-right p-4">
                <el-pagination background layout="total, sizes, prev, pager, next, jumper"
                               :current-page="filterOption.page"
                               :page-size.sync="filterOption.page_size"
                               :total="dataMeta.total"
                               @size-change="filterChange"
                               @current-change="filterPageChange">
                </el-pagination>
            </div>
        </div>
        <el-dialog :title="$t('game.config.currency.dialog.refresh')" center width="320px" :visible.sync="dialog.visible.refresh">
            <div class="text-center" v-html="$t('confirm.data', { status: $t('action.refresh') })"></div>
            <div slot="footer">
                <el-button type="primary" @click="refresh">{{ $t('action.confirm') }}</el-button>
                <el-button @click="dialog.visible.refresh = false">{{ $t('action.cancel') }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            loading: false,
            filterOption: {
                type: null,
                status: null,
                datetime: null,
                datetime_column: 'created',
                keyword: '',
                keyword_column: 'currency',
                page: 1,
                page_size: 100,
                order: 'desc',
                order_key: 'descending',
                order_column: 'created',
            },
            dataMeta: {
                total: 0,
            },
            dataList: [],
            itemCache: {},
            dialog: {
                visible: {
                    refresh: false,
                }
            },
        }
    },
    created() {
        this.filterChange();
    },
    methods: {
        clearFilterOption() {
            this.loading = true;
            this.filterOption.page = 1;
        },
        filterChange() {
            this.clearFilterOption();
            this.getDataList();
        },
        filterPageChange(page) {
            this.loading = true;
            this.filterOption.page = page;
            this.getDataList();
        },
        filterOrderChange(scope) {
            this.clearFilterOption();
            this.filterOption.order = scope.order == 'ascending' ? 'asc' : 'desc';
            this.filterOption.order_key = scope.order;
            this.filterOption.order_column = scope.prop;
            this.getDataList();
        },
        getDataList() {
            axios.get('/game/config/currency/getList', {
                params: this.filterOption
            }).then((response) => {
                if (response.data.resp_msg.code == 200) {
                    this.dataMeta = response.data.resp_data.meta;
                    this.dataList = response.data.resp_data.data;
                } else {
                    this.$message({
                        type: 'error',
                        message: response.data.resp_msg.message,
                        showClose: true
                    });
                }
                this.loading = false;
            })
        },
        // 显示模态框（刷新币种）
        dialogRefresh(scope) {
            // 缓存数据
            this.itemCache = scope.row;
            // 显示模态框
            this.dialog.visible.refresh = true;
        },
        // 刷新币种
        refresh() {
            // 关闭模态框
            this.dialog.visible.refresh = false;
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/game/config/currency/refresh', {
                id: this.itemCache.id,
            }).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.refresh')}),
                        showClose: true
                    });
                    // 修改状态
                    this.itemCache.updated = response.data.resp_data.updated;
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.refresh')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
    },
}
</script>

<style lang="scss">

</style>
